# Guess, Andrew and Alexander Coppock. Does Counter-Attitudinal Information Cause Backlash? Results from Three Large Survey Experiments. British Journal of Political Science, forthcoming.

# Produces Table 7, Table 8, and Table 9 in the main text
# Produdes Tables 3, 4, 13, 14 in the Appendix

# Study 3 (Capital Punishment)

rm(list = ls())
library(tidyverse)
library(reshape2)
library(estimatr)
library(stargazer)
library(xtable)

load("GC_study_3.rdata")
source("GC_helpers.R")



# Table 7 -----------------------------------------------------------------

tab_1 <- 
  study_3  %>%
  group_by(condition_factor) %>%
  summarise(n = format_num(n(), 0),
            positive_information = format_num(mean(positive_information), 0),
            negative_information = format_num(mean(negative_information), 0))

study_3_sub <- study_3 %>% select(condition_factor, support_recode_T2, deter_recode_T2)

tab_2 <- 
  study_3_sub  %>%
  group_by(condition_factor) %>%
  summarise_all(funs(mean, sd))%>%
  mutate_if(is_bare_numeric, format_num, digits = 2)

tab_3 <- 
  map(1:1000, ~study_3_sub %>% sample_n(size = nrow(study_3_sub), replace = TRUE)) %>%
  map(~ group_by(., condition_factor) %>% summarise_all(funs(mean, sd))) %>%
  bind_rows(.id = "sim") %>%
  melt(id.vars = c("sim", "condition_factor")) %>%
  dcast(condition_factor ~ variable, fun.aggregate = sd) %>%
  mutate_if(is_bare_numeric, add_parens, digits = 2)

summary_df <- 
  tab_1 %>%
  left_join(tab_2) %>%
  bind_rows(tab_3) %>%
  mutate(condition_factor = factor(condition_factor, levels = c("CC", "CN", "NN", "PC", "PN", "PP"),
                                   labels=c("Con Con", "Con Null", "Null Null", "Pro Con", "Pro Null", "Pro Pro"))) %>%
  arrange(condition_factor)

summary_df <- 
  within(summary_df,{
    condition_factor[is.na(n)] <- NA
  })

summary_df <- summary_df[,c("condition_factor", "n", "positive_information", "negative_information" ,
                            "support_recode_T2_mean","support_recode_T2_sd", 
                            "deter_recode_T2_mean","deter_recode_T2_sd")]

print.xtable(
  xtable(summary_df),
  hline.after = c(),
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE
)

# study_3 %>%
#   filter(pro %in% c(0, 1)) %>%
#   group_by(pro, condition_factor) %>%
#   summarize(est_1 = mean(support_recode_T2, na.rm = TRUE),
#             se_1 = se_mean(support_recode_T2),
#             est_2 = mean(deter_recode_T2, na.rm = TRUE),
#             se_2 = se_mean(deter_recode_T2),
#             dif_est_1 = mean(support_recode_T2 - support_recode_T1, na.rm = TRUE),
#             dif_se_1 = se_mean(support_recode_T2 - support_recode_T1),
#             dif_est_2 = mean(deter_recode_T2 - deter_recode_T1, na.rm = TRUE),
#             dif_se_2 = se_mean(deter_recode_T2 - deter_recode_T1),
#             entry_1 = paste0(format_num(est_1, 2), add_parens(se_1, 2)),
#             entry_2 = paste0(format_num(est_2, 2), add_parens(se_2, 2)),
#             entry_3 = paste0(format_num(dif_est_1, 2), add_parens(dif_se_1, 2)),
#             entry_4 = paste0(format_num(dif_est_2, 2), add_parens(dif_se_2, 2)))%>%
#   select(pro, condition_factor, entry_1, entry_2, entry_3, entry_4)





# Table 8 -----------------------------------------------------------------

fit_1 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, pro==1))
fit_2 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN") + support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==1))
fit_3 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, pro==0))
fit_4 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==0))

fit_5 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, pro==1))
fit_6 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==1))
fit_7 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, pro==0))
fit_8 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ + race, data=subset(study_3, pro==0))

sink("Written Drafts/BJPS_R&R/tables/dosage_support.tex")
stargazer(fit_1, fit_2, fit_3, fit_4, 
          se=starprep(fit_1, fit_2, fit_3, fit_4),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("(support_recode_T1|deter_recode_T1|age|female|ideology|educ|race)"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Proponents", "Among Opponents"),
          column.separate = c(2,2),
          dep.var.labels="Dependent Variable: T2 Attitude Toward Capital Punishment",
          covariate.labels=c("Positive Information (0 to 2)", "Negative Information (0 to 2)", 
                             "Condition: Null Null", "Constant"),
          align=TRUE,model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Null Null condition is coded 0.",
                  "Covariates include T1 Attitude, T1 Belief, age, gender, ideology, race, and education."),
          font.size="small",label="tab: dosagesupport",float=FALSE)
sink()


# Table 9 -----------------------------------------------------------------

stargazer(fit_5, fit_6, fit_7, fit_8, 
          se=starprep(fit_5, fit_6, fit_7, fit_8),
          p=fix_ps(fit_5, fit_6, fit_7, fit_8),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("support_recode_T1|deter_recode_T1|age|female|ideology|educ|race") , omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Proponents", "Among Opponents"),
          column.separate = c(2,2),
          dep.var.labels="Dependent Variable: T2 Belief in Deterrent Effect",
          covariate.labels=c("Positive Information (0 to 2)", "Negative Information (0 to 2)", 
                             "Condition: Null Null", "Constant"),
          align=TRUE,model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Null Null condition is coded 0.",
                  "Covariates include T1 Attitude, T1 Belief, age, gender, ideology, race, and education."),
          font.size="small",float=FALSE)



# Appendix Tables: Table 3 (Extremity) ------------------------------------

study_3 <-
  within(study_3, {
    qnts <-
      cut(
        support_recode_T1 + deter_recode_T1,
        breaks = quantile(support_recode_T1 + deter_recode_T1, probs = c(0, .25, .5, .75, 1), na.rm = TRUE),
        labels = c(-2,-1, 1, 2)
      )
    extr <- as.numeric(qnts %in% c(-2, 2))
  })


fit_1 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, extr==1))
fit_2 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN") + support_recode_T1 + deter_recode_T1 + age + female + ideology + educ, data=subset(study_3, extr==1))
fit_3 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, extr==0))
fit_4 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ, data=subset(study_3, extr==0))

fit_5 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, extr==1))
fit_6 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ, data=subset(study_3, extr==1))
fit_7 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, extr==0))
fit_8 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + ideology + educ, data=subset(study_3, extr==0))


stargazer(fit_1, fit_2, fit_3, fit_4, 
          se=starprep(fit_1, fit_2, fit_3, fit_4),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("(support_recode_T1|deter_recode_T1|age|female|ideology|educ|race)"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Extreme Initial Position","Moderate Initial Position"),
          column.separate = c(2,2),
          dep.var.labels="Dependent Variable: T2 Attitude Toward Capital Punishment",
          covariate.labels=c("Positive Information (0 to 2)", "Negative Information (0 to 2)", 
                             "Condition: Null Null", "Constant"),
          align=TRUE,model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Null Null condition is coded 0.",
                  "Covariates include T1 Attitude, T1 Belief, age, gender, ideology, and education."),
          font.size="small",label="tab: dosagesupport",float=FALSE)


# Appendix Tables: Table 4 (Extremity) ------------------------------------
stargazer(fit_5, fit_6, fit_7, fit_8, 
          se=starprep(fit_5, fit_6, fit_7, fit_8),
          p=fix_ps(fit_5, fit_6, fit_7, fit_8),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("support_recode_T1|deter_recode_T1|age|female|ideology|educ|race") , omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Extreme Initial Position","Moderate Initial Position"),
          column.separate = c(2,2),
          dep.var.labels="Dependent Variable: T2 Belief in Deterrent Effect",
          covariate.labels=c("Positive Information (0 to 2)", "Negative Information (0 to 2)", 
                             "Condition: Null Null", "Constant"),
          align=TRUE,model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Null Null condition is coded 0.",
                  "Covariates include T1 Attitude, T1 Belief, age, gender, ideology, and education."),
          font.size="small",float=FALSE)


# Appendix Tables: Table 13 (Ideology) ------------------------------------

fit_1 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, ideology=="liberal"))
fit_2 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN") + support_recode_T1 + deter_recode_T1 + age + female + pro + educ + race, data=subset(study_3, ideology=="liberal"))
fit_3 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, ideology=="moderate"))
fit_4 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + pro + educ + race, data=subset(study_3, ideology=="moderate"))
fit_5 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, ideology=="conservative"))
fit_6 <- lm(support_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + pro + educ, data=subset(study_3, ideology=="conservative"))

fit_7 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, ideology=="liberal"))
fit_8 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN") + support_recode_T1 + deter_recode_T1 + age + female + pro + educ + race, data=subset(study_3, ideology=="liberal"))
fit_9 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, ideology=="moderate"))
fit_10 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + pro + educ + race, data=subset(study_3, ideology=="moderate"))
fit_11 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN"), data=subset(study_3, ideology=="conservative"))
fit_12 <- lm(deter_recode_T2 ~ positive_information + negative_information + (condition_factor=="NN")+ support_recode_T1 + deter_recode_T1 + age + female + pro + educ, data=subset(study_3, ideology=="conservative"))


stargazer(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6, 
          se=starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6, type = "HC2"),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6, type = "HC2"),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("(support_recode_T1|deter_recode_T1|age|female|pro|educ|race)"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Liberals","Among Moderates","Among Conservatives"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Attitude Toward Capital Punishment",
          covariate.labels=c("Positive Information (0 to 2)", "Negative Information (0 to 2)", 
                             "Condition: Null Null", "Constant"),
          align=TRUE,model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Null Null condition is coded 0.",
                  "Covariates include T1 Attitude, T1 Belief, age, gender, proponent, race, and education."),
          font.size="small",label="tab: dosagesupport",float=FALSE)

# Appendix Tables: Table 14 (Ideology) ------------------------------------
stargazer(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12, 
          se=starprep(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12),
          p=fix_ps(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("(support_recode_T1|deter_recode_T1|age|female|pro|educ|race)"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Liberals","Among Moderates","Among Conservatives"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Belief in Deterrent Effect",
          covariate.labels=c("Positive Information (0 to 2)", "Negative Information (0 to 2)", 
                             "Condition: Null Null", "Constant"),
          align=TRUE,model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Null Null condition is coded 0.",
                  "Covariates include T1 Attitude, T1 Belief, age, gender, proponent, race, and education."),
          font.size="small",float=FALSE)


